package com.itboye.mengwu.widget;

/*
 * Copyright (c) 2014, �ൺ˾ͨ�Ƽ����޹�˾ All rights reserved.
 * File Name��InnerListview.java
 * Version��V1.0
 * Author��zhaokaiqiang
 * Date��2014-8-15
 */

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ListView;

/**
 * 
 * @ClassName: com.example.listdemo.InnerListview
 * @Description: ���Է���Listview�е�Listview
 * @author zhaokaiqiang
 * @date 2014-8-15 ����2:43:34
 * 
 */
public class InnerListview extends ListView {
	GestureDetector gestureDetector;

	public InnerListview(Context context) {
		super(context);
		gestureDetector = new GestureDetector(context, new Gesture(context));
	}

	public InnerListview(Context context, AttributeSet attrs) {
		super(context, attrs);
		gestureDetector = new GestureDetector(context, new Gesture(context));
	}

	public InnerListview(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		gestureDetector = new GestureDetector(context, new Gesture(context));

	}
	float old_x;
	float old_y;
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// if(gestureDetector.onTouchEvent(ev)) return true;

		System.out.println("-----switch------");
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			//			if (this.getFirstVisiblePosition() == 0)
			//				setParentScrollAble(true);
			//			else
			//				setParentScrollAble(false);
			break;
		case MotionEvent.ACTION_MOVE:
			// setParentScrollAble(false);
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			setParentScrollAble(true);
			//			
			break;

		}
		return super.onInterceptTouchEvent(ev);

	}
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			//			if (this.getFirstVisiblePosition() == 0)
			//				setParentScrollAble(true);
			//			else
			//				setParentScrollAble(false);
			old_x=ev.getX();
			old_y=ev.getY();
			break;
		case MotionEvent.ACTION_MOVE:
			System.out.println("-----move------");
			if(this.getFirstVisiblePosition() != 0&&Math.abs(old_y-ev.getY())/Math.abs(old_x-ev.getX())>3)
			{
				System.out.println("-----init------");
				setParentScrollAble(false);
			}
			// setParentScrollAble(false);
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			setParentScrollAble(true);
			//			
			break;

		}
		return super.onTouchEvent(ev);
	}
	private void setParentScrollAble(boolean flag) {
		getParent().requestDisallowInterceptTouchEvent(!flag);
	}



}
